home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Turbo Pascal V7 / TVFM.ZIP / DIRVIEW.PAS < prev    next >
Pascal/Delphi Source File  |  1992-11-03  |  9KB  |  360 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision File Manager Demo               }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. {$X+}
  9.  
  10. unit DirView; { directory pane }
  11.  
  12. interface
  13.  
  14. uses Drivers, Objects, Views, Outline, Dos;
  15.  
  16. type
  17.   PDirectory = ^TDirectory;
  18.   TDirectory = object(TObject)
  19.     Dir: PString;
  20.     SubDirectories: Boolean;
  21.     Children: PDirectory;
  22.     Next: PDirectory;
  23.     constructor Init(const ADir: String);
  24.     destructor Done; virtual;
  25.     procedure Adjust(Expand: Boolean);
  26.     function Expanded: Boolean;
  27.     function GetSubdirectory(I: Integer): PDirectory;
  28.     function GetName: String;
  29.     function GetNumSubdirectories: Integer;
  30.   end;
  31.  
  32. type
  33.   PDirectoryViewer = ^TDirectoryViewer;
  34.   TDirectoryViewer = object(TOutlineViewer)
  35.     SearchPos, OldFoc: Integer;
  36.     Root: PDirectory;
  37.     constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar;
  38.       ARoot: PDirectory);
  39.     destructor Done; virtual;
  40.     procedure HandleEvent(var Event: TEvent); virtual;
  41.     procedure Adjust(Node: Pointer; Expand: Boolean); virtual;
  42.     function GetRoot: Pointer; virtual;
  43.     function GetNumChildren(Node: Pointer): Integer; virtual;
  44.     function GetChild(Node: Pointer; I: Integer): Pointer; virtual;
  45.     function GetText(Node: Pointer): String; virtual;
  46.     function IsExpanded(Node: Pointer): Boolean; virtual;
  47.     function HasChildren(Node: Pointer): Boolean; virtual;
  48.     function GetPalette: PPalette; virtual;
  49.   end;
  50.  
  51. implementation
  52.  
  53. uses App, Equ, Globals, Tools;
  54.  
  55. const
  56.   CDirectoryViewer = CScroller + #3#8;
  57.  
  58. { TDirectory }
  59.  
  60. constructor TDirectory.Init(const ADir: String);
  61. var
  62.   SR: SearchRec;
  63. begin
  64.   inherited Init;
  65.   Dir := NewStr(ADir);
  66.   Next := nil;
  67.   Children := nil;
  68.  
  69.   { See if any subdirectories exist in given directory }
  70.   FindFirst(Dir^ + '\*.*', Directory, SR);
  71.   while DosError = 0 do
  72.   begin
  73.     if (SR.Attr and Directory <> 0) and (SR.Name[1] <> '.') then
  74.     begin
  75.       SubDirectories := True;
  76.       Exit;
  77.     end;
  78.     FindNext(SR);
  79.   end;
  80.   SubDirectories := False;
  81. end;
  82.  
  83. destructor TDirectory.Done;
  84. begin
  85.   if Children <> nil then Dispose(Children, Done);
  86.   if Next <> nil then Dispose(Next, Done);
  87.   DisposeStr(Dir);
  88.   inherited Done;
  89. end;
  90.  
  91. procedure TDirectory.Adjust(Expand: Boolean);
  92. var
  93.   SR: SearchRec;
  94.   PCur: ^PDirectory;
  95. begin
  96.   if Expand then
  97.   begin
  98.     PCur := @Children;
  99.     FindFirst(Dir^ + '\*.*', Directory, SR);
  100.     while DosError = 0 do
  101.     begin
  102.       if (SR.Attr and Directory <> 0) and (SR.Name[1] <> '.') then
  103.       begin
  104.         PCur^ := New(PDirectory, Init(Dir^ + '\' + SR.Name));
  105.         PCur := @PCur^^.Next;
  106.       end;
  107.       FindNext(SR);
  108.     end;
  109.     PCur^ := nil;
  110.   end
  111.   else
  112.   begin
  113.     if Children <> nil then Dispose(Children, Done);
  114.     Children := nil;
  115.   end;
  116. end;
  117.  
  118. function TDirectory.GetNumSubdirectories: Integer;
  119. var
  120.   I: Integer;
  121.   Cur: PDirectory;
  122. begin
  123.   I := 0;
  124.   Cur := Children;
  125.   while Cur <> nil do
  126.   begin
  127.     Cur := Cur^.Next;
  128.     Inc(I);
  129.   end;
  130.   GetNumSubdirectories := I;
  131. end;
  132.  
  133. function TDirectory.GetSubdirectory(I: Integer): PDirectory;
  134. var
  135.   Cur: PDirectory;
  136. begin
  137.   Cur := Children;
  138.   while (Cur <> nil) and (I <> 0) do
  139.   begin
  140.     Cur := Cur^.Next;
  141.     Dec(I);
  142.   end;
  143.   GetSubdirectory := Cur;
  144. end;
  145.  
  146. function TDirectory.GetName: String;
  147. var
  148.   ADir: DirStr;
  149.   AName: NameStr;
  150.   AExt: ExtStr;
  151. begin
  152.   FSplit(Dir^, ADir, AName, AExt);
  153.   if (AName = '') and (AExt = '') then GetName := ADir
  154.   else GetName := AName + AExt;
  155. end;
  156.  
  157. function TDirectory.Expanded: Boolean;
  158. begin
  159.   Expanded := Children <> nil;
  160. end;
  161.  
  162. { TDirectoryViewer }
  163.  
  164. constructor TDirectoryViewer.Init(var Bounds: TRect; AHScrollBar,
  165.   AVScrollBar: PScrollBar; ARoot: PDirectory);
  166. begin
  167.   inherited Init(Bounds, AHScrollBar, AVScrollBar);
  168.   Root := ARoot;
  169.   Update;
  170.   SearchPos := 0;
  171.   OldFoc := 0;
  172.   SetCursor(0, 0);
  173.   ShowCursor;
  174. end;
  175.  
  176. destructor TDirectoryViewer.Done;
  177. begin
  178.   Dispose(Root, Done);
  179.   inherited Done;
  180. end;
  181.  
  182. procedure TDirectoryViewer.HandleEvent(var Event: TEvent);
  183. var
  184.   SearchStr: String;
  185.   Lev, Pos: Integer;
  186.   Lns: LongInt;
  187.   Flgs: Word;
  188.   Dir: PDirectory;
  189.   Mover: PFileMover;
  190.   Where: TPoint;
  191.  
  192.   function UpStr(S: String): String;
  193.   var
  194.     I: Integer;
  195.   begin
  196.     for I := 1 to Length(S) do
  197.       S[I] := UpCase(S[I]);
  198.     UpStr := S;
  199.   end;
  200.  
  201.   function IsAMatch(Cur: Pointer; Level, Position: Integer;
  202.     Lines: LongInt; Flags: Word): Boolean; far;
  203.   var
  204.     S: String;
  205.   begin
  206.     IsAMatch := False;
  207.     if UpStr(Copy(GetText(Cur),1, Length(SearchStr))) = SearchStr then
  208.     begin
  209.       IsAMatch := True;
  210.       Pos := Position;
  211.       Lev := Level;
  212.       Lns := Lines;
  213.       Flgs := Flags;
  214.     end;
  215.   end;
  216.  
  217.   function GetGraphParams(Cur: Pointer; Level, Position: Integer;
  218.     Lines: LongInt; Flags: Word): Boolean; far;
  219.   begin
  220.     GetGraphParams := False;
  221.     if Position = Foc then
  222.     begin
  223.       Lev := Level;
  224.       Lns := Lines;
  225.       Flgs := Flags;
  226.       GetGraphParams := True;
  227.     end;
  228.   end;
  229.  
  230. begin
  231.   inherited HandleEvent(Event);
  232.   if Event.What = evBroadcast then
  233.   begin
  234.     case Event.Command of
  235.       cmGetCurrentDir:
  236.         begin
  237.           Dir := GetNode(Foc);
  238.           PString(Event.InfoPtr)^ := Dir^.Dir^;
  239.           ClearEvent(Event);
  240.         end;
  241.       cmItemDropped: 
  242.         begin
  243.           Mover := Event.InfoPtr;
  244.           if MouseInView(Mover^.Origin) then
  245.           begin
  246.             ClearEvent(Event);
  247.             MakeLocal(Mover^.Origin, Where);
  248.             Dir := GetNode(Where.Y + 1 + Delta.Y);
  249.             DragDropCopy(Mover, Dir^.Dir^);
  250.           end;
  251.         end;
  252.       else
  253.         Exit;
  254.     end;
  255.   end;
  256.  
  257.   if (Event.What <> evBroadcast) and (Foc <> OldFoc) then
  258.     SearchPos := 0;
  259.   Pos := -1;
  260.   case Event.What of
  261.     evKeyDown:
  262.       begin
  263.         if (Event.KeyCode = kbBack) or
  264.       ((Event.ScanCode <> 0) and
  265.        (Event.CharCode in ['A'..'Z','a'..'z', '0'..'9'])) then
  266.         begin
  267.           if SearchPos > 0 then
  268.           begin
  269.             SearchStr := UpStr(GetText(GetNode(Foc)));
  270.             SearchStr[0] := Char(SearchPos);
  271.           end else SearchStr := '';
  272.           if Event.KeyCode = kbBack then
  273.           begin
  274.             if Length(SearchStr) > 0 then Dec(SearchStr[0])
  275.             else Exit;
  276.           end
  277.           else if Length(SearchStr) < 255 then
  278.           begin
  279.             Inc(SearchStr[0]);
  280.             SearchStr[Length(SearchStr)] := UpCase(Event.CharCode);
  281.           end;
  282.           if FirstThat(@IsAMatch) <> nil then
  283.           begin
  284.             Focused(Pos);
  285.             SearchPos := Length(SearchStr);
  286.             Update;
  287.             DrawView;
  288.           end else Pos := -1;
  289.           ClearEvent(Event);
  290.         end;
  291.         if Event.CharCode = '\' then
  292.         begin
  293.           Dir := PDirectory(GetNode(Foc));
  294.           if (not Dir^.Expanded) and HasChildren(Dir) then
  295.           begin
  296.             Dir^.Adjust(True);
  297.             Update;
  298.             DrawView;
  299.             ClearEvent(Event);
  300.           end;
  301.         end;
  302.       end;
  303.   end;
  304.   if (Foc <> OldFoc) or (Pos <> -1) then
  305.   begin
  306.     if Pos = -1 then
  307.       FirstThat(@GetGraphParams);
  308.     SetCursor(Length(GetGraph(Lev, Lns, Flgs)) + SearchPos,
  309.       Foc - Delta.Y);
  310.     Dir := GetNode(Foc);
  311.     Message(Desktop, evBroadcast, cmNewDir, Dir^.Dir);
  312.     OldFoc := Foc;
  313.   end;
  314. end;
  315.  
  316.  
  317. procedure TDirectoryViewer.Adjust(Node: Pointer; Expand: Boolean);
  318. begin
  319.   PDirectory(Node)^.Adjust(Expand);
  320. end;
  321.  
  322. function TDirectoryViewer.GetRoot: Pointer;
  323. begin
  324.   GetRoot := Root;
  325. end;
  326.  
  327. function TDirectoryViewer.GetNumChildren(Node: Pointer): Integer;
  328. begin
  329.   GetNumChildren := PDirectory(Node)^.GetNumSubDirectories;
  330. end;
  331.  
  332. function TDirectoryViewer.GetChild(Node: Pointer; I: Integer): Pointer;
  333. begin
  334.   GetChild := PDirectory(Node)^.GetSubdirectory(I);
  335. end;
  336.  
  337. function TDirectoryViewer.GetText(Node: Pointer): String;
  338. begin
  339.   GetText := PDirectory(Node)^.GetName;
  340. end;
  341.  
  342. function TDirectoryViewer.IsExpanded(Node: Pointer): Boolean;
  343. begin
  344.   IsExpanded := PDirectory(Node)^.Expanded;
  345. end;
  346.  
  347. function TDirectoryViewer.HasChildren(Node: Pointer): Boolean;
  348. begin
  349.   HasChildren := PDirectory(Node)^.SubDirectories;
  350. end;
  351.  
  352. function TDirectoryViewer.GetPalette: PPalette;
  353. const
  354.   NewPal: string[Length(CDirectoryViewer)] = CDirectoryViewer;
  355. begin
  356.   GetPalette := @NewPal;
  357. end;
  358.  
  359. end.
  360.